package org.wishwork.api.stock.in.box.request;

import cn.hutool.core.util.StrUtil;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

/**
 * @description: 商品列表
 * @author: WangYX
 * @create: 2022-04-11 20:48
 * @Version: 1.0.0
 **/
@EqualsAndHashCode
@ToString
@Getter
public class Item {

    /**
     * 箱号，最大 20
     */
    private String packId;

    /**
     * 商品编码
     */
    private String skuId;

    /**
     * 数量
     */
    private Integer qty;

    /**
     * 唯一码，此编码存在时将生成系统唯一码，并强制数量为1
     */
    private String skuSn;


    private Item(String packId, String skuId, Integer qty, String skuSn) {
        this.packId = packId;
        this.skuId = skuId;
        this.qty = qty;
        this.skuSn = skuSn;
    }

    public static Builder builder(){
        return new Builder();
    }

    public static class Builder {
        private String packId;
        private String skuId;
        private Integer qty;
        private String skuSn;

        public Builder packId(String packId) {
            if (StrUtil.isNotEmpty(packId)) {
                int length = packId.length();
                if (length > 20) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "pickId max length is 20!");
                }
            }
            this.packId = packId;
            return this;
        }

        public Builder skuId(String skuId) {
            if (StrUtil.isEmpty(skuId)) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "skuId must be not null or blank!");
            }
            this.skuId = skuId;
            return this;
        }

        public Builder qty(Integer qty) {
            if (qty == null) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "qty must be not null or blank!");
            }
            this.qty = qty;
            return this;
        }

        public Builder skuSn(String skuSn) {
            this.skuSn = skuSn;
            return this;
        }

        public Item build() {
            if (StrUtil.isEmpty(this.skuId)) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "skuId not set!");
            }
            if (qty == null) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "qty not set!");
            }
            return new Item(this.packId, this.skuId, this.qty, this.skuSn);
        }

    }

}
